package com.wangrui.typehandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

@MappedTypes({String.class})
@MappedJdbcTypes({JdbcType.BINARY, JdbcType.VARBINARY})
public class BinaryTypeHandler extends BaseTypeHandler<String> {

    @Override
    public String getNullableResult(ResultSet arg0, String arg1) throws SQLException {
        byte[] bytes = arg0.getBytes(arg1);
        return new String(bytes);
    }

    @Override
    public String getNullableResult(ResultSet arg0, int arg1) throws SQLException {
        byte[] bytes = arg0.getBytes(arg1);
        return new String(bytes);
    }

    @Override
    public String getNullableResult(CallableStatement arg0, int arg1) throws SQLException {
        byte[] bytes = arg0.getBytes(arg1);
        return new String(bytes);
    }

    @Override
    public void setNonNullParameter(PreparedStatement arg0, int arg1, String arg2, JdbcType arg3) throws SQLException {
        arg0.setBytes(arg1, arg2.getBytes());
    }

}
